<?php

/* @project : Template
 * @auther  : 青云
 * @date    : 2018/1/10
 * @desc    : 菜单控制器
 */

namespace app\admin\controller;

use app\common\model\SystemGroup as SystemGroupModel;
use app\common\model\SystemGroupData as SystemGroupDataModel;
use think\Request;

class SystemGroupData extends Base
{
    function index(Request $request)
    {
        $group_id = $request->group_id;
        $this->assign('group_id', $group_id);

        $group = SystemGroupModel::get($group_id);
        $this->assign('group', $group);

        $field_list = json_decode($group['fields'], true);
        $this->assign('field_list', $field_list);

        $request->eq_group_id = $group_id;
        $request->_sort = 'sort asc';

        $this->_index($request);
        return $this->fetch();
    }

    function add(Request $request)
    {
        $group_id = $request->group_id;
        $this->assign('group_id', $group_id);

        $group = SystemGroupModel::get($group_id);
        $this->assign('group', $group);

        $field_list = json_decode($group['fields'], true);
        $this->assign('field_list', $field_list);

        return $this->fetch();
    }

    function edit(Request $request)
    {
        $vo = $this->_edit($request);

        $group_id = $vo->group_id;
        $this->assign('group_id', $group_id);

        $group = SystemGroupModel::get($group_id);
        $this->assign('group', $group);

        $field_list = json_decode($group['fields'], true);
        $this->assign('field_list', $field_list);

        return $this->fetch();
    }

    function save(Request $request)
    {
        $group_id = $request->group_id;
        $this->assign('group_id', $group_id);

        $group = SystemGroupModel::get($group_id);
        $this->assign('group', $group);

        $field_list = json_decode($group['fields'], true);
        $this->assign('field_list', $field_list);

        $data = $request->param();
        $value = [];
        foreach ($field_list as $field) {
            if (isset($data[$field['field_code']])) {
                $item['field_type'] = $field['field_type'];
                $item['value'] = $data[$field['field_code']];
                $value[$field['field_code']] = $item;
            }
        }
        $request->value = json_encode($value);
        return $this->_save($request);
    }

    function select_pid()
    {

        $where_menu[] = array('is_del', 'eq', 0);
        $menu_list = model('SystemGroupData')->field('id,pid,name,sort')->where($where_menu)->order('sort asc')->select();

        $root['id'] = 0;
        $root['name'] = '根节点';

        if (!$menu_list->isEmpty()) {
            $menu_tree = list_to_tree($menu_list->toArray());
            $root['_child'] = $menu_tree;
        }

        $this->assign('menu_tree', array($root));

        return $this->fetch();
    }

    function del(Request $request)
    {
        $result = $this->_destory($request);
        if ($result) {
            return $this->api_success('删除成功');
        } else {
            return $this->api_error('删除失败');
        }
    }

    function sort_up(Request $request)
    {
        $vo = SystemGroupDataModel::get($request->id);
        $map[] = ['group_id', 'eq', $vo->group_id];
        return $this->_sort_up($request, $map);
    }

    function sort_down(Request $request)
    {
        $vo = SystemGroupDataModel::get($request->id);
        $map[] = ['group_id', 'eq', $vo->group_id];
        return $this->_sort_down($request, $map);
    }
}
